package net.vinote.smart.platform.service.action;

import java.sql.SQLException;
import java.text.DecimalFormat;

import net.vinote.smart.platform.comm.PlatformConst;
import net.vinote.smart.platform.module.database.DBManager;
import net.vinote.smart.platform.module.database.DBManagerFactory;
import net.vinote.smart.platform.module.database.DBTransaction;
import net.vinote.smart.platform.module.database.bo.BizObject;
import net.vinote.smart.platform.service.SRE;
import net.vinote.smart.platform.service.util.AutoInjection;
import net.vinote.smart.platform.service.util.Permission;
import net.vinote.smart.platform.service.util.Permission.PermissionEnum;

/**
 * 代码明细Action类
 * 
 * @author 郑俊伟
 */
public class CodeLibraryAction {
	@AutoInjection
	private String codeNo;

	@AutoInjection
	private String itemNo;

	/**
	 * 刷新权限表
	 * 
	 * @param tx
	 * @return
	 * @throws SQLException
	 */
	@Permission(PermissionEnum.SMART_ADMINISTRATOR)
	public String refreshPermission(DBTransaction tx) throws SQLException {
		DBManager bm = DBManagerFactory.getInstance("code_library");
		tx.join(bm);
		DecimalFormat format = new DecimalFormat("9000");
		int i = 1;
		for (PermissionEnum permission : PermissionEnum.values()) {
			bm.delete(new String[] { "CodeNo", "ItemNo" }, "AuthGroup",
					permission.getCode());// 清除表数据
			BizObject bo = new BizObject();
			bo.setAttribute("CodeNo", "AuthGroup");// 代码类目
			bo.setAttribute("ItemNo", permission.getCode());// 权限值
			bo.setAttribute("ItemName", permission.getName());// 权限名称
			bo.setAttribute("IsInUse", PlatformConst.YES);// 描述列的注释
			bo.setAttribute("SORTNO", format.format(i));// 设置排序号
			i++;
			bm.saveBizObject(bo);
		}
		return SRE.getInstance().initializ() ? PlatformConst.SUCCESS
				: PlatformConst.FAIL;
	}
}
